Skip to content

gh-161962: Fix LazyLoader deadlock on circular imports between threads - #161990

Open
tanveer-arch wants to merge 2 commits into
python:mainfrom
tanveer-arch:fix-lazyloader-deadlock-import-cycle
Open

tanveer-arch wants to merge 2 commits into
python:mainfrom
tanveer-arch:fix-lazyloader-deadlock-import-cycle

Conversation

@tanveer-arch

@tanveer-arch tanveer-arch commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #161962.

LazyLoader serialized the lazy load with a plain per-module threading.RLock, which has no deadlock detection. If two lazily loaded modules import each other and two threads first-touch a different one, each thread takes its own module's lock and then blocks on the other's lock forever. The same modules imported eagerly do not deadlock, because the import system's module lock detects the cycle and raises _DeadlockError, and the partially initialized module is used instead.

This makes the lazy load use the import system's module lock (_bootstrap._ModuleLock) too. On a _DeadlockError it proceeds without the lock, exactly as _lock_unlock_module() does for eager imports, so the thread already loading the module returns the partially initialized module through the existing is_loading branch. The per-module RLock and the deferred threading import in exec_module() are removed.

Adds a regression test (test_lazy_circular_import_no_deadlock) that loads two lazily loaded modules in a cycle from two threads, with a barrier for deterministic timing, and asserts both complete with the expected values. The test deadlocks without the fix.

The fix follows the approach suggested by @djrobstep in the issue. AI assistance disclosure: I used an AI tool to help investigate and build this fix; I reviewed the change and the test in detail and verified them by building CPython from main and running the importlib test suite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

importlib.util.LazyLoader deadlocks when two threads first-touch modules in an import cycle

1 participant